package com.lds.sign_sdk.util;

import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.http.HttpMethod;

/**
 * @Date 2022/1/7
 * @Time 15:32
 * @Author liudongs
 * @E-mail liudongs@aliyun.com
 * @Version 1.0.0
 * @Description
 **/
public class HttpUtils {
    public HttpUtils() {
    }

    public static SortedMap<String, String> getAllParams(HttpServletRequest request) throws IOException {
        SortedMap<String, String> result = new TreeMap();
        Map<String, String> urlParams = getUrlParams(request);
        Iterator var3 = urlParams.entrySet().iterator();

        while(var3.hasNext()) {
            Entry entry = (Entry)var3.next();
            result.put((String)entry.getKey(), (String)entry.getValue());
        }

        Map<String, String> allRequestParam = new HashMap(16);
        if (!HttpMethod.GET.name().equals(request.getMethod())) {
            allRequestParam = getAllRequestParam(request);
        }

        if (allRequestParam != null) {
            Iterator var7 = ((Map)allRequestParam).entrySet().iterator();

            while(var7.hasNext()) {
                Entry entry = (Entry)var7.next();
                result.put((String)entry.getKey(), (String)entry.getValue());
            }
        }

        return result;
    }

    public static Map<String, String> getAllRequestParam(HttpServletRequest request) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
        String str = "";
        StringBuilder wholeStr = new StringBuilder();

        while((str = reader.readLine()) != null) {
            wholeStr.append(str);
        }

        return (Map)JSONObject.parseObject(wholeStr.toString(), Map.class);
    }

    public static Map<String, String> getUrlParams(HttpServletRequest request) {
        String param = "";

        try {
            param = URLDecoder.decode(request.getQueryString(), "utf-8");
        } catch (UnsupportedEncodingException var9) {
            var9.printStackTrace();
        }

        Map<String, String> result = new HashMap(16);
        String[] params = param.split("&");
        String[] var4 = params;
        int var5 = params.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String s = var4[var6];
            int index = s.indexOf("=");
            result.put(s.substring(0, index), s.substring(index + 1));
        }

        return result;
    }

    public static String getBodyString(HttpServletRequest request) throws IOException {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;

        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            char[] bodyCharBuffer = new char[1024];
            boolean var5 = false;

            int len;
            while((len = reader.read(bodyCharBuffer)) != -1) {
                sb.append(new String(bodyCharBuffer, 0, len));
            }
        } catch (IOException var18) {
            var18.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }
            }

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException var16) {
                    var16.printStackTrace();
                }
            }

        }

        return sb.toString();
    }

    public static void render(HttpServletResponse httpServletResponse, String message) throws Exception {
        httpServletResponse.setStatus(203);
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        OutputStream outputStream = httpServletResponse.getOutputStream();
        outputStream.write(message.getBytes("UTF-8"));
        outputStream.flush();
        outputStream.close();
    }
}

